package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.RegularMsgSend;
import com.ruoyi.system.service.IRegularMsgSendService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 定时发送消息记录Controller
 * 
 * @author DEREK
 * @date 2024-03-02
 */
@RestController
@RequestMapping("/system/regularMsg")
public class RegularMsgSendController extends BaseController
{
    @Autowired
    private IRegularMsgSendService regularMsgSendService;

    /**
     * 查询定时发送消息记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:regularMsg:list')")
    @GetMapping("/list")
    public TableDataInfo list(RegularMsgSend regularMsgSend)
    {
        startPage();
        List<RegularMsgSend> list = regularMsgSendService.selectRegularMsgSendList(regularMsgSend);
        return getDataTable(list);
    }

    /**
     * 导出定时发送消息记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:regularMsg:export')")
    @Log(title = "定时发送消息记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RegularMsgSend regularMsgSend)
    {
        List<RegularMsgSend> list = regularMsgSendService.selectRegularMsgSendList(regularMsgSend);
        ExcelUtil<RegularMsgSend> util = new ExcelUtil<RegularMsgSend>(RegularMsgSend.class);
        util.exportExcel(response, list, "定时发送消息记录数据");
    }

    /**
     * 获取定时发送消息记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:regularMsg:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(regularMsgSendService.getById(id));
    }

    /**
     * 新增定时发送消息记录
     */
    @PreAuthorize("@ss.hasPermi('system:regularMsg:add')")
    @Log(title = "定时发送消息记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RegularMsgSend regularMsgSend)
    {
        return toAjax(regularMsgSendService.save(regularMsgSend));
    }

    /**
     * 修改定时发送消息记录
     */
    @PreAuthorize("@ss.hasPermi('system:regularMsg:edit')")
    @Log(title = "定时发送消息记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RegularMsgSend regularMsgSend)
    {
    	if(regularMsgSend.getTimeDay() <=0 && regularMsgSend.getTimeHour() <=0 && regularMsgSend.getTimeMinute() <=0 && regularMsgSend.getTimeSecond() <= 0) {
    		return AjaxResult.error("时间必须存在一个");
    	}
        return toAjax(regularMsgSendService.updateById(regularMsgSend));
    }

    /**
     * 删除定时发送消息记录
     */
    @PreAuthorize("@ss.hasPermi('system:regularMsg:remove')")
    @Log(title = "定时发送消息记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(regularMsgSendService.deleteRegularMsgSendByIds(ids));
    }
}
